#include "../ex4/fileperf.hh"

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <string>
#include <math.h>

#include <cstdio>

using namespace std;

#define POINTER_SIZE (4)
#define MAXIMAL_FILE_SIZE ( 100000000 )


#define MIN_BLOCK_SIZE (40)
#define BLOCK_INTERVAL (10)
#define MAX_BLOCK_SIZE (0x500)

//#define MIN_BLOCK_SIZE (100)
//#define BLOCK_INTERVAL (100)
//#define MAX_BLOCK_SIZE (10000)


int main(int argc, char** argv)
{
	uint block_size, pointer_size, maximal_size;
	storageType type;
	int res = 1;
	int node_res, fat_res;
	outputStruct out_FAT, out_iNode;
	vector<uint> sizes;
	string line;
	ofstream access_file;
	ofstream space_file;

	access_file.precision(6);
	access_file.setf(ios::fixed,ios::floatfield);

	access_file.open( "access.txt", ios_base::out | ios_base::app);
	space_file.open( "space.txt", ios_base::out | ios_base::app);

	if ( access_file.fail() || space_file.fail() )
	{
		cerr << "Error while opening output files - EXIT!" << endl;
		goto end;
	}

	do
	{
		cin >> line;
		if ( ! cin )
		{
			break;
		}
		sizes.push_back( atoi( line.c_str() ) );
	}
	while ( 1 );

	cin.clear();

	for ( uint i = MIN_BLOCK_SIZE; i <= MAX_BLOCK_SIZE; i += BLOCK_INTERVAL )
	{
		fat_res = fileSystemPerformance (i,
										 POINTER_SIZE,
										 MAXIMAL_FILE_SIZE,
										 FS_LINKED_LIST,
										 sizes,
										 & out_FAT);

		node_res = fileSystemPerformance (i,
				 	 	 	 	 	 	  POINTER_SIZE,
				 	 	 	 	 	 	  MAXIMAL_FILE_SIZE,
				 	 	 	 	 	 	  FS_INODE,
				 	 	 	 	 	 	  sizes,
				 	 	 	 	 	 	  & out_iNode);


		if ( fat_res || node_res )
		{
			cerr << "Error: fileSystemPerformance() returned non-zero value! - Exit!" << endl ;
			break;
		}

		access_file << i << " " << out_FAT.averageAccessNum << " " << out_iNode.averageAccessNum << endl;
		space_file << i << " " << out_FAT.space << " " << out_iNode.space << endl;
	}



end:

	if ( access_file.is_open())
	{
		access_file.close();
	}

	if ( space_file.is_open())
	{
		space_file.close();
	}

	return res;
}
